package com.blocks57.homework.service.impl;

import com.blocks57.homework.dto.ProductDTO;
import com.blocks57.homework.exception.DataNotFoundException;
import com.blocks57.homework.exception.DuplicateElementException;
import com.blocks57.homework.model.Product;
import com.blocks57.homework.model.ShoppingCartItem;
import com.blocks57.homework.dto.ShoppingCartItemDTO;
import com.blocks57.homework.repository.ProductRepository;
import com.blocks57.homework.repository.ShoppingCartItemRepository;
import com.blocks57.homework.repository.UserRepository;
import com.blocks57.homework.service.ShoppingCartItemService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * ShoppingCartItemServiceImpl
 *
 * @author Tom at 2023-10-29
 */
@Service
@Transactional
public class ShoppingCartItemServiceImpl implements ShoppingCartItemService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ShoppingCartItemRepository shoppingCartItemRepository;

    @Autowired
    private DozerBeanMapper dozerBeanMapper;

    public String saveShoppingCartItem(ShoppingCartItemDTO shoppingCartItemDTO, Long userId) throws DuplicateElementException {

        List<ShoppingCartItem> shoppingCartItemList = shoppingCartItemRepository.findByUserIdAndProductId(userId, shoppingCartItemDTO.getProductId());
        ShoppingCartItem shoppingCartItem;
        if(shoppingCartItemList.isEmpty()){
            shoppingCartItem = new ShoppingCartItem();
            Optional<Product> optionalProduct = productRepository.findById(shoppingCartItemDTO.getProductId());
            Product product = optionalProduct.get();
            shoppingCartItem.setUserId(userId);
            shoppingCartItem.setProductId(product.getProductId());
            shoppingCartItem.setAmount(shoppingCartItemDTO.getAmount());
            shoppingCartItemRepository.save(shoppingCartItem);
            shoppingCartItemRepository.flush();
            return "SUCCESS";
        } else {
            throw new DuplicateElementException("Product with productId "+ shoppingCartItemDTO.getProductId() + " already present");
        }

    }
    public List<ShoppingCartItemDTO> findAll(Long userId) {
        List<ShoppingCartItem> shoppingCartItemList = shoppingCartItemRepository.findByUserId(userId);
        List<ShoppingCartItemDTO> shoppingCartItemDTOList = new ArrayList<>();
        for(ShoppingCartItem shoppingCartItem: shoppingCartItemList){
            ShoppingCartItemDTO shoppingCartItemDTO = dozerBeanMapper.map(shoppingCartItem, ShoppingCartItemDTO.class);
            shoppingCartItemDTOList.add(shoppingCartItemDTO);
        }
        return shoppingCartItemDTOList;
    }

    public String updateShoppingCartItem(ShoppingCartItemDTO shoppingCartItemDTO, Long userId) throws DataNotFoundException {

//        Optional<User> userOptional= userRepository.findById(userId);
        List<ShoppingCartItem> shoppingCartItemList = shoppingCartItemRepository.findByUserIdAndProductId(userId, shoppingCartItemDTO.getProductId());
        ShoppingCartItem shoppingCartItem;
        if(!shoppingCartItemList.isEmpty()){
            shoppingCartItem = shoppingCartItemList.get(0);
            shoppingCartItem.setAmount(shoppingCartItemDTO.getAmount());
            shoppingCartItemRepository.save(shoppingCartItem);
            shoppingCartItemRepository.flush();
            return "SUCCESS";
        } else {
            throw new DataNotFoundException("ShoppingCartItem Not Found");
        }
    }

    public String deleteShoppingCartItem(Long shoppingCartItemId, Long userId) throws DataNotFoundException {
        List<ShoppingCartItem> shoppingCartItemList = shoppingCartItemRepository.findByUserIdAndShoppingCartItemId(userId, shoppingCartItemId);
        if(!shoppingCartItemList.isEmpty()){
            shoppingCartItemRepository.deleteById(shoppingCartItemList.get(0).getShoppingCartItemId());
            return "SUCCESS";
        } else{
            throw new DataNotFoundException("ShoppingCartItem Not Found");
        }
    }

    public String clearShoppingCart(Long userId) {
        shoppingCartItemRepository.deleteAll(shoppingCartItemRepository.findByUserId(userId));
        return "SUCCESS";
    }

}
